package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SupplierService;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Supplier;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class SupplierServiceImpl implements SupplierService {


    @Autowired
    private IBaseDao iBaseDao;


    @Autowired
    private PageQuerDao pageQuerDao;

    /**
     * 添加供应商
     *
     * @param sup_uuid    供应商唯一标识
     * @param sup_name    供应商名称
     * @param sup_address 地址
     * @param sup_attn    联系人
     * @param sup_phone   电话
     * @param sup_mail    邮箱
     * @param sup_remark  备注
     */
    @Override
    public ZhongkaiResult addSupplier(String sup_uuid, String sup_name, String sup_address, String sup_attn, String sup_phone, String sup_mail,String sup_remark) {
        if (StringUtils.isNullOrEmpty(sup_uuid) || StringUtils.isNullOrEmpty(sup_name)
                ) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        Supplier supplier;
/*        Map<String, Object> params = new HashMap<>();   //防止sql注入
        params.put("supName", sup_name);*/
        supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.supName='" + sup_name + "'");
        //   System.out.println(count);
        if (supplier != null) {
            return ZhongkaiResult.build(401, "供应商已存在");
        }
        try {
            supplier = new Supplier();
            supplier.setSupUuid(sup_uuid);
            supplier.setSupName(sup_name);
            supplier.setSupAddress(sup_address);
            supplier.setSupAttn(sup_attn);
            supplier.setSupPhone(sup_phone);
            supplier.setSupMail(sup_mail);
            supplier.setSupRemark(sup_remark);



            long millis = new DateTime().getMillis(); //获取当前系统时间戳
            supplier.setSupTime(millis);
            supplier.setSupRemove("1");

            iBaseDao.save(supplier);

            return ZhongkaiResult.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(401, "添加失败");
        }
    }


    /**
     * 删除供应商信息
     *
     * @param id 供应商id
     */
    @Override
    public ZhongkaiResult removeSupplier(Long id) {
        if (id == 0) {
            return ZhongkaiResult.build(402, "数据不完整");
        }

        Supplier supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.id='" + id + "'");

        if (supplier == null) {
            return ZhongkaiResult.build(402, "此供应商不存在");
        }

        try {
            supplier.setSupRemove("2");

            iBaseDao.update(supplier);

            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(402, "删除失败");
        }

    }


    /**
     * 修改供应商
     *
     * @param id          供应商唯一标识
     * @param sup_name    供应商名称
     * @param sup_address 地址
     * @param sup_attn    联系人
     * @param sup_phone   电话
     * @param sup_mail    邮箱
     * @param sup_remark  备注
     */
    @Override
    public ZhongkaiResult updateSupplier(String id, String sup_name, String sup_address, String sup_attn, String sup_phone, String sup_mail,String sup_remark) {

        if (StringUtils.isNullOrEmpty(id) ||
                StringUtils.isNullOrEmpty(sup_name)||
                StringUtils.isNullOrEmpty(sup_address)||
                StringUtils.isNullOrEmpty(sup_attn)||
                StringUtils.isNullOrEmpty(sup_phone)||
                StringUtils.isNullOrEmpty(sup_mail)||
                StringUtils.isNullOrEmpty(sup_remark)
                ) {

            return ZhongkaiResult.build(400, "数据不完整");
        }


        Supplier supplier;
/*        Map<String, Object> params = new HashMap<>();
        params.put("supName", sup_name);*/
        supplier = (Supplier) iBaseDao.get("from Supplier sp where sp.id='" + id + "'");
        //   System.out.println(count);
        if (supplier == null) {
            return ZhongkaiResult.build(401, "供应商不存在");
        }
        try {
            supplier.setSupName(sup_name);
            supplier.setSupAddress(sup_address);
            supplier.setSupAttn(sup_attn);
            supplier.setSupPhone(sup_phone);
            supplier.setSupMail(sup_mail);
            supplier.setSupRemark(sup_remark);

/*            long millis = new DateTime().getMillis(); //获取当前系统时间戳
            supplier.setSupTime(millis);*/
            // supplier.setSupRemove("1");

            iBaseDao.update(supplier);

            return ZhongkaiResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(401, "修改失败");
        }

    }


    /**
     * 分页获取供应商信息
     *
     * @param page 当前页数
     * @param rows 每页数据条数
     */
    @Override
    public ZhongkaiResult listSupplier(Integer page, Integer rows) {

        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Supplier.class);
            detachedCriteria.add(Restrictions.eq("supRemove", "1")); //增加条件
            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }
}
